You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

50 lines
1.2 KiB

import { requireAdmin } from "#server/utils/admin-guard";
import { getProviderById } from "#server/service/llm";
export default defineWrappedResponseHandler(async (event) => {
await requireAdmin(event);
const id = Number(getRouterParam(event, "id"));
if (!id) {
return R.throwError(400, "无效的供应商 ID", null);
}
const provider = await getProviderById(id);
if (!provider) {
return R.throwError(404, "供应商不存在", null);
}
const baseUrl = provider.baseUrl?.replace(/\/+$/, "") || "";
if (!baseUrl) {
return R.success([]);
}
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (provider.parseMode === "anthropic") {
if (provider.apiKey) {
headers["x-api-key"] = provider.apiKey;
}
headers["anthropic-version"] = "2023-06-01";
} else {
if (provider.apiKey) {
headers["Authorization"] = `Bearer ${provider.apiKey}`;
}
}
try {
const res = await $fetch<{ data?: { id: string; name?: string }[] }>(
`${baseUrl}/models`,
{ headers, timeout: 8000 },
);
const models = (res?.data ?? []).map((m) => ({
id: m.id,
name: m.name || m.id,
}));
return R.success(models);
} catch {
return R.success([]);
}
});